package Both;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;

import Serveur.Serialization;

/**
 * Cette classe permet de gerer les moyens de transport
 * 
 * @author Fabien
 * 
 */
public class Transport implements Serializable {

    // Nom court pour le moyen de transport
    private String nom;

    // Numero du transport
    private int numTrans;

    // Description du moyen de transport
    private String description;

    // Capacit� du moyen de transport
    private int capacite;

    /**
     * Constructeur normal
     * 
     * @param nom
     * @param description
     * @param capacit
     *            �
     */
    public Transport(String nom, String description, int num, int capacite) {
        super();

        // remplacer les / present dans le code
        nom.replace(Serialization.CAR_SPACE, Serialization.CAR_REPLACE);
        description.replace(Serialization.CAR_SPACE, Serialization.CAR_REPLACE);

        this.nom = nom;
        this.description = description;
        this.capacite = capacite;
        numTrans = num;
    }

    @Override
    public String toString() {
        String retour;
        retour = "Nom : " + nom;
        retour += "	Description : " + description;
        retour += "	Numero : " + numTrans;
        retour += "	Capacit� : " + capacite;
        return retour;
    }

    public void print() {
        System.out.println(toString());
    }

    @Override
    public boolean equals(Object arg0) {
        // TODO Auto-generated method stub
        Transport trans = (Transport) arg0;
        if (!(trans.getCapacite() == capacite)) {
            return false;
        }
        if (!trans.getDescription().equals(description)) {
            return false;
        }
        if (!trans.getNom().equals(nom)) {
            return false;
        }

        return true;

    }

    public String getNom() {
        return nom;
    }

    public String getDescription() {
        return description;
    }

    public int getCapacite() {
        return capacite;
    }

    /**
     * Rechercher un Transport dans le serveur
     * 
     * @param ListeTransport
     * @param nom
     * @return
     */
    public static ArrayList<Transport> searchTransport(
            ArrayList<Transport> ListeTransport, String nom) {
        ArrayList<Transport> ListeRetour = new ArrayList<Transport>();

        Iterator<Transport> it = ListeTransport.iterator();
        while (it.hasNext()) {
            Transport current = it.next();
            if (current.getNom().equalsIgnoreCase(nom)) {
                ListeRetour.add(current);
            }
        }
        return ListeRetour;
    }

    public int getNumTrans() {
        return numTrans;
    }

    /**
     * Rechercher un transport dans le serveur
     * 
     * @param ListeTransport
     * @param T
     * @return
     */
    public static ArrayList<Transport> searchTransport(
            ArrayList<Transport> ListeTransport, Transport T) {
        ArrayList<Transport> ListeRetour = new ArrayList<Transport>();

        Iterator<Transport> it = ListeTransport.iterator();
        while (it.hasNext()) {
            Transport current = it.next();
            if (current.equals(T)) {
                ListeRetour.add(current);
            }
        }

        return ListeRetour;
    }

    /**
     * Recherche un transport par son numero
     * 
     */
    public static Transport getTransport(ArrayList<Transport> ListeTranport,
            int num) {
        Iterator<Transport> it = ListeTranport.iterator();
        while (it.hasNext()) {
            Transport t = it.next();
            if (t.getNumTrans() == num) {
                return t;
            }
        }
        return null;
    }
}
